﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using Kivensoft.XlsReader;

namespace Kivensoft.Helper {
	/*
		var headers = new string[] { "姓名", "年龄", "电话", "家庭住址", "余额" };
		var f = File.Open(@"z:\测试.xlsx", FileMode.Open, FileAccess.Read);
		var reader = new XlsxImport(f, headers);
		while (reader.NextRow()) {
			Console.WriteLine("{0}, {1}, {2}, {3}, {4}\n", 
				reader.CellAsString(0),
				reader.CellAsInt(1),
				reader.CellAsDateTime(2),
				reader.CellAsDate(3),
				reader.CellAsDecimal(4));
		}
		f.Close();
	*/
	public class XlsxImport {
		private Workbook workbook;
		private Sheet sheet;
		private Row currentRow;
		private int currentRowNum;

		public Workbook Workbook { get { return workbook; } }
		public Sheet Sheet { get { return sheet; } }
		public Row CurrentRow { get { return currentRow; } }
		public int CurrentRowNum { get { return currentRowNum; } }
		public int RowCount { get { return sheet.Rows.Count; } }
		public int ColCount { get { return sheet.Columns.Count; } }
		public int CurrentRowCount { get { return currentRow.Cells.Count; } }

		public XlsxImport(string fileName, string[] headers = null) {
			workbook = new Workbook(fileName);
			FindHeaders(headers);
		}

		public XlsxImport(Stream input, string[] headers = null) {
			workbook = new Workbook(input);
			FindHeaders(headers);
		}

		private void FindHeaders(string[] headers) {
			//校验工作表
			var sheets = workbook.Sheets;
			var sheetsCount = sheets.Count();
			if (sheetsCount == 0)
				throw new Exception("未找到工作表！");
			if (sheetsCount > 1)
				throw new Exception("工作表数量大于1个！");
			sheet = sheets.First();
			currentRowNum = -1;

			if (headers == null) return;

			var hlen = headers.Length;
			while (NextRow()) {
				var r = currentRow;
				var matched = true;
				for (int i = 0; i < hlen; i++) {
					var h = headers[i];
					if (!string.IsNullOrEmpty(h) && !h.Equals(r.CellAsString(i))) {
						matched = false;
						break;
					}
				}
				if (matched) return;
			}

			StringBuilder sb = new StringBuilder(512);
			sb.Append("未找到指定的列头[");
			foreach (var item in headers)
				sb.Append(item).Append(',');
			sb.Remove(sb.Length - 1, 1).Append("]!");
			throw new Exception(sb.ToString());
		}

		public bool NextRow() {
			if (++currentRowNum >= sheet.Rows.Count) return false;
			else {
				currentRow = sheet.Rows[currentRowNum];
				return currentRow == null ? false : true;
			}
		}

		public bool HasMore {
			get { return currentRowNum + 1 >= sheet.Rows.Count ? false : true; }
		}

		public string CellAsString(int index) {
			return currentRow == null ? null : currentRow.CellAsString(index);
		}

		public int? CellAsInt(int index) {
			return currentRow == null ? null : currentRow.CellAsInt(index);
		}

		public double? CellAsDouble(int index) {
			return currentRow == null ? null : currentRow.CellAsDouble(index);
		}

		public decimal? CellAsDecimal(int index) {
			return currentRow == null ? null : currentRow.CellAsDecimal(index);
		}

		public DateTime? CellAsDate(int index) {
			return currentRow == null ? null : currentRow.CellAsDate(index);
		}

		public TimeSpan? CellAsTime(int index) {
			return currentRow == null ? null : currentRow.CellAsTime(index);
		}

		public DateTime? CellAsDateTime(int index) {
			return currentRow == null ? null : currentRow.CellAsDateTime(index);
		}
	}
}
